using System;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

namespace HareGame
{
    public class HeadTips : MonoBehaviour_Ex
    {
        [SerializeField]
        private Image _imgProgress;

        [SerializeField]
        private Text _txtMsg;

        private void LateUpdate()
        {
            transform.localScale = Vector3.one * 0.015f;
            transform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, Camera.main.transform.up);
        }

        /// <summary>
        /// 显示进度条
        /// </summary>
        /// <param name="time">耗时</param>
        /// <param name="callback">回调</param>
        public Tweener ShowProgress(float time, Action callback)
        {
            _imgProgress.transform.parent.gameObject.SetActive(true);
            _imgProgress.fillAmount = 0;
            return _imgProgress.DOFillAmount(1, time).SetEase(Ease.Linear).OnComplete(() =>
                 {
                     _imgProgress.transform.parent.gameObject.SetActive(false);
                     callback?.Invoke();
                 });
        }

        /// <summary>
        /// 显示钱
        /// </summary>
        /// <param name="value">数值</param>
        public void ShowPay(double value)
        {
            GameEntry.Data.AddMoney(value);
            _txtMsg.transform.parent.gameObject.SetActive(true);
            _txtMsg.GetComponentInParent<CanvasGroup>().DOFade(1, 0).SetUpdate(true);
            _txtMsg.text = "+" + value.double2String();
            _txtMsg.transform.DOLocalMoveY(45, 1).SetEase(Ease.Linear).SetUpdate(true);
            _txtMsg.GetComponentInParent<CanvasGroup>().DOFade(0, 1).SetEase(Ease.Linear).OnComplete(() =>
            {
                _txtMsg.transform.parent.gameObject.SetActive(false);
                _txtMsg.transform.DOLocalMoveY(-15, 0);
            }).SetUpdate(true);
        }
    }
}